﻿using System.Collections;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Data;
using Mota.EditModule;


namespace Mota.XMLControls
{
    public abstract class GuiControl
    {
        
        internal Object visualControl = null;
        private bool required = true;
        private Dictionary<String, String> _xmlAttributes = null;
        private String name = String.Empty;
        protected Dictionary<string, GuiControl> mainControls = new Dictionary<string, GuiControl>();
        public event EventHandler OnCustomEvent;
        public event EventHandler OnChange;
        public abstract object GetValue();
        public void AddMain(string fieldName, GuiControl mainControl)
        {
            this.mainControls[fieldName] = mainControl;
        }
        public string Name
        {
            get
            {
                if (name == String.Empty)
                {
                    name = Guid.NewGuid().ToString();
                }
                return name;
            }
            set
            {
                if (value != String.Empty)
                    name = value;
            }
        }
        public Dictionary<String, String> XmlAttributes
        {
            get
            {
                return _xmlAttributes;
            }
        }
        public abstract String Text { get; set; }
        public abstract void Add(GuiControl guiControl);
        // mainBs, mainBs.DataSource = mainDataSet, mainBs.DataMember = realViewName
        public virtual void Refresh(BindingSource mainBs)
        {
            if (this.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                this.SetValue(mainBs);
            }
        }
        public abstract IEnumerator GetEnumerator();
        public abstract void Accept(GuiVisitor visitor);

        public abstract object Binding { get; set; }
        public abstract void SetValue(object valueBinding);
        public GuiControl()
        {
            _xmlAttributes = new Dictionary<String, String>();
        }

        public virtual int TabIndex
        {
            set
            {
                if (this.visualControl is Control)
                    (this.visualControl as Control).TabIndex = value;
            }
            get
            {
                if (this.visualControl is Control)
                    return (this.visualControl as Control).TabIndex;
                else
                    return 0;
            }
        }
        public virtual bool IsSimple
        {
            //if we need to show toolbar set to false value
            get
            {
                return true;
            }
        }
        public virtual void SetPropertyValue(String propertyName, String propertyValue)
        {
            // chesh null or empty
            if (String.IsNullOrEmpty(propertyValue) || String.IsNullOrEmpty(propertyName))
                return;

            // add all properties to dictionary for future use if needed
            _xmlAttributes[propertyName] = propertyValue;
            // choose properties only from allowed 
            switch (propertyName)
            {
                case "Name":
                    Name = propertyValue;
                    break;

                case "Text":
                    Text = propertyValue;
                    break;

                case "Left":
                    int parsedLeft;
                    if (int.TryParse(propertyValue, out parsedLeft))
                    {
                        Left = parsedLeft;
                    }
                    break;

                case "Top":
                    int parsedTop;
                    if (int.TryParse(propertyValue, out parsedTop))
                    {
                        Top = parsedTop;
                    }
                    break;

                case "Enabled":
                    bool parsedEnabled;
                    if (bool.TryParse(propertyValue, out parsedEnabled))
                    {
                        Enabled = parsedEnabled;
                    }
                    break;

                case "Required":
                    bool parsedRequired;
                    if (bool.TryParse(propertyValue, out parsedRequired))
                    {
                        Required = parsedRequired;
                    }
                    break;
                case "Width":
                    int parsedWidth;
                    if(int.TryParse(propertyValue,out parsedWidth))
                    {
                        Width = parsedWidth;
                    }
                    break;

                case "Dock":
                    DockStyle parsedValue;
                    if (DockStyle.TryParse(propertyValue, out parsedValue))
                    {
                        Dock = parsedValue;
                    }
                    break;
            }
        }

        protected virtual void CustomEventCall(EventArgs args)
        {
            if (OnCustomEvent != null)
                OnCustomEvent(this, (args!=null)?args:EventArgs.Empty);
        }
        protected virtual void ChangeEventCall(EventArgs args)
        {
            if (OnChange != null)
                OnChange(this, (args != null) ? args : EventArgs.Empty);
        }
        public int Width
        {
            get
            {
                return (visualControl as Control).Width;
            }
            set
            {
                (visualControl as Control).Width = value;
            }
        }

        public int Left
        {
            get
            {
                return (visualControl as Control).Left;
            }
            set
            {
                (visualControl as Control).Left = value;
            }
        }

        public int Top
        {
            get
            {
                return (visualControl as Control).Top;
            }
            set
            {
                (visualControl as Control).Top = value;
            }
        }
        public bool Enabled
        {
            get
            {
                return (visualControl as Control).Enabled;
            }
            set
            {
                (visualControl as Control).Enabled = value;
            }
        }

        public virtual bool Required
        {
            get
            {
                return this.required;
            }
            set
            {
                this.required = value;
            }
        }


        public virtual DockStyle Dock
        {
            get
            {
                return (visualControl as Control).Dock;
            }
            set
            {
                (visualControl as Control).Dock = value;
            }
        }
    }
}
